package com.xxx.kjds.serviceImpl;

import com.xxx.kjds.entity.FinanceCheck;
import com.xxx.kjds.entity.FinanceCheckExample;
import com.xxx.kjds.entity.TraceKey;
import com.xxx.kjds.mapper.FinanceCheckMapper;
import com.xxx.kjds.service.FinanceService;
import com.xxx.kjds.service.TraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceCheckMapper financeCheckMapper;
    @Autowired
    private TraceService traceService;

    @Override
    public long countByExample(FinanceCheckExample example) {
        return financeCheckMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(FinanceCheckExample example) {
        return financeCheckMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return financeCheckMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FinanceCheck record) {
        return financeCheckMapper.insert(record);
    }

    @Override
    public int insertSelective(FinanceCheck record) {
        return financeCheckMapper.insertSelective(record);
    }

    @Override
    public List<FinanceCheck> selectByExample(FinanceCheckExample example) {
        return financeCheckMapper.selectByExample(example);
    }

    @Override
    public FinanceCheck selectByPrimaryKey(Integer id) {
        return financeCheckMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(FinanceCheck record, FinanceCheckExample example) {
        return financeCheckMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(FinanceCheck record, FinanceCheckExample example) {
        return financeCheckMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(FinanceCheck record) {
        return financeCheckMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FinanceCheck record) {
        return financeCheckMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<FinanceCheck> getList() {
        FinanceCheckExample FinanceCheckExample = new FinanceCheckExample();
        FinanceCheckExample.Criteria criteria = FinanceCheckExample.createCriteria();
        criteria.andIdIsNotNull();
        List<FinanceCheck> list = financeCheckMapper.selectByExample(FinanceCheckExample);
        return list;
    }

    @Override
    public List<FinanceCheck> selectByWid(Integer wid) {
        //结果list
        List<FinanceCheck> result = new ArrayList<>();

        //根据wid查trace表，得到fid
        List<TraceKey> traceList = traceService.selectByWid(wid);

        //根据fid查finance表
        for (TraceKey traceKey : traceList) {
            Integer fid = traceKey.getFcId();

            FinanceCheckExample FinanceCheckExample = new FinanceCheckExample();
            FinanceCheckExample.Criteria criteria = FinanceCheckExample.createCriteria();
            criteria.andIdEqualTo(fid);
            List<FinanceCheck> financeCheckList = financeCheckMapper.selectByExample(FinanceCheckExample);
            result.add(financeCheckList.get(0));
        }

        return result;
    }

    @Override
    public FinanceCheck selectByApplyTime(Date time) {
        FinanceCheckExample FinanceCheckExample = new FinanceCheckExample();
        FinanceCheckExample.Criteria criteria = FinanceCheckExample.createCriteria();
        criteria.andApplyTimeEqualTo(time);
        List<FinanceCheck> financeCheckList = financeCheckMapper.selectByExample(FinanceCheckExample);
        if (financeCheckList.size() > 0) {
            return financeCheckList.get(0);
        }
        return null;
    }

    /*@Override
    public FinanceCheck selectLast() {
        FinanceCheckExample FinanceCheckExample = new FinanceCheckExample();
        FinanceCheckExample.Criteria criteria = FinanceCheckExample.createCriteria();
        criteria.and
        List<FinanceCheck> financeCheckList = financeCheckMapper.selectByExample(FinanceCheckExample);
        if(financeCheckList.size()>0){
            return financeCheckList.get(0);
        }
        return null;
    }*/
}
